package com.suwadi.logger.transactionlogger;

public class TransactionIndicatingUtil {
	private final static String TSM_KEY = "org.springframework.transaction.support.TransactionSynchronizationManager";

	public static String getTransactionStatus(boolean verbose) {
		String status;
		ClassLoader contextClassLoader = Thread.currentThread()
				.getContextClassLoader();
		if (contextClassLoader != null) {
			try {
				Class tsmClass = contextClassLoader.loadClass(TSM_KEY);
				Boolean isActive = (Boolean) tsmClass.getMethod(
						"isActualTransactionActive", null).invoke(null, null);
				if (!verbose) {
					System.out.println("*************************" + isActive);
					status = (isActive) ? "+" : "-";
				} else {
					String transactionName = (String) tsmClass.getMethod(
							"getCurrentTransactionName", null).invoke(null,
							null);
					status = (isActive) ? transactionName : "no transaction";
				}
			} catch (Exception e) {
				status = (verbose) ? "spring unavailable" : "x";
			}
		} else {
			status = (verbose) ? "ccl unavailable" : "x";
		}
		return status;
	}
}
